package reflection;

import cglib.Buyer;

import java.lang.reflect.Method;

/**
 * @Author:zimmor
 * @Date: 2021/3/25 14:33
 * @Description 反射练习
 * @version: 1.0.0
 */

public class ReflectionDemo1 {
    public static void main(String[] args) throws Exception {
        //Class是java在运行时追踪所属类的信息的文件
        //得到对象的Class对象有三种方式1.T.class  2.Class.forName("全限定类名") 3.具体对象的obj.getClass()方法;
        String className = "cglib.Buyer";
        Class<Buyer> buyer = (Class<Buyer>) Class.forName(className);
        //使用Cla  ss的实例对象来new一个有无参构造的对象
        Buyer buyer1 = buyer.newInstance();
        System.out.println("buyer1.buybuybuy() = " + buyer1.buybuybuy());
        Class<Person> personClass = (Class<Person>) Class.forName("reflection.Person");
//        Person person = personClass.newInstance();
        //用Class实例对象来new一个有参构造对象
        Person person = personClass.getConstructor(String.class, String.class).newInstance("tom","18");
        System.out.println(person.toString());
//        Buyer.class.getMethod("buyAK", String.class).invoke(new Buyer(), "47");
        Method buy = Buyer3.class.getDeclaredMethod("buy");
        buy.setAccessible(true);
        buy.invoke(new Buyer3());
    }
}
